package com.jack.helloworld.activity;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.jack.helloworld.R;

import androidx.appcompat.app.AppCompatActivity;

public class GetAccountActivity extends AppCompatActivity {

    private static final String TAG = GetAccountActivity.class.getSimpleName();
    private TextView mTvAccountInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_account);


        mTvAccountInfo = (TextView) findViewById(R.id.tv_account_info);
        String accountInfo = getGoogleAccount(this);
        Log.d(TAG, "onCreate->accountInfo: " + accountInfo);

        mTvAccountInfo.setText(accountInfo);
    }

    public String getGoogleAccount(Context context) {
        StringBuilder sb = new StringBuilder();
        try {
            AccountManager accountManager = AccountManager.get(context);
//            Account[] accounts = accountManager.getAccountsByType("com.google");
            Account[] accounts = accountManager.getAccounts();
            Log.d(TAG, "accounts.length: " + accounts.length);
            for (Account account : accounts) {
                sb.append(account.name).append("\n").append(account.type).append("\n");
            }
        } catch (Exception e) {
        }
        return sb.toString();
    }
}
